package com.czk.tree.huffmantree.zipfile;

import com.czk.tree.huffmantree.huffmancode.HuffmanCode;

import java.io.*;
import java.util.Map;

/**
 * 压缩文件
 * @Author:ChenZhangKun
 * @Date: 2020/7/11 16:17
 */
public class ZipFile {
    public static void main(String[] args) {
        //zipFile("E:\\czk\\jvm\\lecture\\classloader\\3.txt","E:\\czk\\jvm\\lecture\\classloader\\3.zip");

        unZipFile("E:\\czk\\jvm\\lecture\\classloader\\3.zip","E:\\czk\\jvm\\lecture\\classloader\\2.txt");
        System.out.println("解压成功");
    }

    /**
     *
     * @param zipFilePath 解压的文件
     * @param destFile 解压后存储的路径
     */
    public static void unZipFile(String zipFilePath,String destFile){
        // 文件输入流
        InputStream inputStream=null;
        // 对象输入流
        ObjectInputStream objectInputStream=null;
        // 文件输出流
        OutputStream outputStream=null;
        try {
            inputStream=new FileInputStream(zipFilePath);
            // 创建一个对象输入流
            objectInputStream=new ObjectInputStream(inputStream);
            // 读取huffmanCode
            byte[] huffmanCode =(byte[]) objectInputStream.readObject();
            // 读取huffmanMap
            Map<Byte,String> huffmanMap=(Map<Byte,String>)objectInputStream.readObject();
            // 解码
            byte[] decode = HuffmanCode.decode(huffmanMap, huffmanCode);
            outputStream=new FileOutputStream(destFile);
            outputStream.write(decode);
        }catch (IOException |ClassNotFoundException ex){
            ex.printStackTrace();
        }finally {
            try {
                if (inputStream!=null){
                    inputStream.close();
                }
                if (objectInputStream!=null){
                    objectInputStream.close();
                }
                if (outputStream!=null){
                    objectInputStream.close();
                }
            }catch (IOException ex){
                ex.printStackTrace();
            }

        }
    }
    /**
     *
     * @param originPath 原始文件路径
     * @param destPath 压缩后文件路径
     */
    public static void zipFile(String originPath,String destPath){
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        ObjectOutputStream objectOutputStream=null;
        try {
            // 创建输入流
            fileInputStream = new FileInputStream(originPath);
            // 创建和源文件一样大些的字节数组
            byte[] b=new byte[fileInputStream.available()];
            // 读取文件
            fileInputStream.read(b);
            // 得到处理后的赫夫曼数组
            byte[] huffmanCode = HuffmanCode.huffmanZip(b);
            // 创建文件输出流，存放压缩文件
            fileOutputStream=new FileOutputStream(destPath);
            // 创建一个和文件输出流对应的对象输出流
            objectOutputStream=new ObjectOutputStream(fileOutputStream);
            // 写出huffmanBytes,以对象流的方式写入huffmanCode
            objectOutputStream.writeObject(huffmanCode);
            // 将赫夫曼编码一同写入，用于以后解码
            objectOutputStream.writeObject(HuffmanCode.getHuffmanMap());
        }catch (IOException ex){
            ex.printStackTrace();
        }finally {
           try {
               if (fileInputStream!=null){
                   fileInputStream.close();
               }
               if (fileOutputStream!=null){
                   fileOutputStream.close();
               }
               if (objectOutputStream!=null){
                   objectOutputStream.close();
               }
           }catch (IOException ex){
               ex.printStackTrace();
           }
        }

    }
}
